/**
 * Copyright by e-shion.net
 */
package net.xfok.ui.animation;

import java.util.List;

import net.xfok.ui.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ViewFlipper;

/**
 * @author 2010-12-27 Howard
 * 
 */
public class SlideGrid extends Activity implements OnClickListener,OnTouchListener{
	private static final String TAG="SlideGrid";
	private static final int ITEM_COUNT_PORT=21;
	private static final int ITEM_COUNT_LAND=20;
	private static final int COLUMN_WIDTH=80;
	private ViewFlipper vf;
	private float oldTouchValue;
	private int itemCount=ITEM_COUNT_PORT;
	private float mCenterX, mCenterY;
	private List<ResolveInfo>[] apps;
	private List<ResolveInfo> mApps;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.slide);
		vf=(ViewFlipper)findViewById(R.id.flipper);
//		vf.setOnTouchListener(this);
//		vf.setOnClickListener(this);		
	}
	public void onResume(){
		super.onResume();
		mCenterX = getWidth(this) / 2;
		mCenterY = getHeight(this) / 2;	
		itemCount=mCenterX>mCenterY?ITEM_COUNT_LAND:ITEM_COUNT_PORT;
		loadApps();	
		setViewFlipper();
	}
	private void setViewFlipper(){
		for(List<ResolveInfo> mApps:apps){
			GridView grid=new GridView(this);
			grid.setColumnWidth(COLUMN_WIDTH);
			grid.setNumColumns(GridView.AUTO_FIT);
			grid.setAdapter(new AppsAdapter(mApps));
			grid.setPadding(10, 10, 10, 10);
			grid.setVerticalSpacing(10);
			grid.setHorizontalSpacing(10);
			grid.setGravity(Gravity.CENTER);
			grid.setOnTouchListener(this);
//			grid.setOnClickListener(this);
			vf.addView(grid);
		}
	}
	public static int getWidth(Context cx) {
		DisplayMetrics dm = cx.getApplicationContext().getResources()
				.getDisplayMetrics();
		return dm.widthPixels;
	}

	public static int getHeight(Context cx) {
		DisplayMetrics dm = cx.getApplicationContext().getResources()
				.getDisplayMetrics();
		return dm.heightPixels;
	}
	
	private void loadApps() {
		Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
		mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
		mApps = getPackageManager().queryIntentActivities(mainIntent, 0);
		int num=mApps.size()%itemCount==0?mApps.size()/itemCount:mApps.size()/itemCount+1;
		apps=new List[num];
		for(int i=0;i<num;i++){
			int end=(i+1)*itemCount>mApps.size()?mApps.size():(i+1)*itemCount;
			apps[i]=mApps.subList(i*itemCount, end);
		}
	}
	
	@Override
	public boolean onTouch(View v, MotionEvent event) {	
		// TODO Auto-generated method stub
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN: {
			oldTouchValue = event.getX();
			break;
		}
		case MotionEvent.ACTION_UP: {
			float currentX = event.getX();
			Log.i(TAG, "oldTouchValue="+oldTouchValue);
			if (oldTouchValue < currentX) {
				Log.i(TAG, "==========>right:"+event.getX());
				slideRight();
			}
			if (oldTouchValue > currentX) {
				Log.i(TAG, "==========>left:"+event.getX());
				slideLeft();
			}
			break;
		}
		}
		return false;
	}
	
	private void slideLeft(){		
		vf.setInAnimation(SlideAnimation.inFromRightAnimation());
		vf.setOutAnimation(SlideAnimation.outToLeftAnimation());
		vf.showNext();			
	}
	
	private void slideRight(){
		vf.setInAnimation(SlideAnimation.inFromLeftAnimation());
		vf.setOutAnimation(SlideAnimation.outToRightAnimation());
		vf.showPrevious();
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}
	
	public class AppsAdapter extends BaseAdapter {
		private List<ResolveInfo> mApps;
		public AppsAdapter(List<ResolveInfo> mApps) {
			this.mApps=mApps;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView i;

			if (convertView == null) {
				i = new ImageView(SlideGrid.this);
				i.setScaleType(ImageView.ScaleType.FIT_CENTER);
				i.setLayoutParams(new GridView.LayoutParams(50, 50));
			} else {
				i = (ImageView) convertView;
			}
			ResolveInfo info = mApps.get(position);
			i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));
			return i;
		}

		public final int getCount() {
			return mApps.size();
		}

		public final Object getItem(int position) {
			return mApps.get(position);
		}

		public final long getItemId(int position) {
			return position;
		}
	}
}
